﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using LarDosIdosos.Business.Model;
using LarDosIdosos.DataSources;
using Button = System.Web.UI.WebControls.Button;

namespace LarDosIdosos
{
    public partial class Responsavel : System.Web.UI.Page
    {

        private Business.Model.Funcionario funcionario;
        private const string Acesso = "acesso";
        private const string Editar = "editar";

        protected void Page_Load(object sender, EventArgs e)
        {
            CarregaFuncionario();
            if (!ValidaPermissao("acesso"))
            {
                Response.Redirect("~/Default.aspx");
            }
            CarregaBotoesDeCadastro();
        }

        private void CarregaBotoesDeCadastro()
        {
            buttonCadastrar.Visible = ValidaPermissao(Editar);
        }

        private void CarregaFuncionario()
        {
            if (Session["usuario"] != null)
            {
                funcionario = (Business.Model.Funcionario)Session["usuario"];
            }
        }

        //Note: Valida a permissão do funcionário
        private bool ValidaPermissao(string tipo)
        {
            if (funcionario != null)
            {
                switch (tipo)
                {
                    case Acesso:
                        if (funcionario.Cargo_Funcionario.CodigoCargo == 1)
                        {
                            return true;
                        }
                        break;
                    case Editar:
                        if (funcionario.Cargo_Funcionario.CodigoCargo == 1)
                        {
                            return true;
                        }
                        break;
                }
            }
            return false;
        }
		
		protected void buttonConsulta_OnClick(object sender, EventArgs e)
		{
            string busca = textBoxConsulta.Text;
            dataSourceResponsavel.SelectParameters.Clear();
            Parameter param = new Parameter();
            param.Name = "sql";
            if (busca != "")
            {

                param.DefaultValue = "select * from responsavel where nome_responsavel like '%" + busca + "%'";
            }
            else
            {
                param.DefaultValue = "select * from responsavel";
            }
            dataSourceResponsavel.SelectParameters.Add(param);
            //dataSourceResponsavel.SelectParameters.Add(
            //List<Business.Model.Responsavel> responsaveis = dataSourceResponsavel.BuscaResponsaveis(0,10,"","select * from responsavel where nome_responsavel like '%" + busca + "%'");
            GridView grid = (GridView) updatePanelResponsavel.FindControl("gridViewResponsavel");
            grid.DataBind();
		}

        protected void gridViewResponsavel_DataBound(object sender, EventArgs e)
        {
            
        }

        protected void gridViewResponsavel_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
            {
                ImageButton editar = (ImageButton)e.Row.FindControl("imageButtonEditar");
                ImageButton excluir = (ImageButton)e.Row.FindControl("imageButtonExcluir");
                editar.Visible = excluir.Visible = ValidaPermissao(Editar);

                DataSourceResponsavel dataSourceResponsavel = new DataSourceResponsavel();
                Business.Model.Responsavel responsavel = (Business.Model.Responsavel) e.Row.DataItem;
                Repeater repeater = (Repeater) e.Row.FindControl("repeaterIdosos");
                repeater.DataSource = dataSourceResponsavel.BuscaIdososDoResponsavel(responsavel.Codigo_Responsavel);
                repeater.DataBind();
            }
        }
		

        protected void gridViewResponsavel_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "visualizar")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gridViewResponsavel.Rows[index];
                HiddenField hiddenField = (HiddenField)row.FindControl("hiddenFieldId");
                if (hiddenField != null) MontaPaginaComDetalhes(Convert.ToInt32(hiddenField.Value), false);

            }
            if (e.CommandName == "editar")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gridViewResponsavel.Rows[index];
                HiddenField hiddenField = (HiddenField)row.FindControl("hiddenFieldId");
                if (hiddenField != null) MontaPaginaComDetalhes(Convert.ToInt32(hiddenField.Value), true);

            }
            if (e.CommandName == "excluir")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gridViewResponsavel.Rows[index];
                HiddenField hiddenField = (HiddenField)row.FindControl("hiddenFieldId");
                if (hiddenField != null) ExcluiRegistro(Convert.ToInt32(hiddenField.Value));

            }
        }

        private void MontaPaginaComDetalhes(int Codigo, bool habilita)
        {
            DataSourceResponsavel dataSourceResponsavel = new DataSourceResponsavel();
            Business.Model.Responsavel responsavel = dataSourceResponsavel.BuscaResponsavel(Codigo);
            webUserControlResponsavel.CarregaVisualizacaoDetalhada(responsavel,habilita);
            panelVisualizarResponsavel.Visible = true;
            panelResponsavel.Visible = false;
        }

        private void ExcluiRegistro(int Codigo_Responsavel)
        {
//            var result = MessageBox.Show("Deseja excluir?", "Excluindo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly, false);
//            if (result == DialogResult.Yes)
//            {
//                DataSourceResponsavel dataSourceResponsavel = new DataSourceResponsavel();
//                dataSourceResponsavel.Delete(Codigo_Responsavel);
//
//                gridViewResponsavel.DataBind();
//            }
			DataSourceResponsavel dataSourceResponsavel = new DataSourceResponsavel();
			dataSourceResponsavel.Delete(Codigo_Responsavel);
			
			gridViewResponsavel.DataBind();
        }

        protected void gridViewResponsavel_OnSorted(object sender, EventArgs e)
        {
            
        }

        protected void gridViewResponsavel_OnPageIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void ddPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void linkButtonVoltar_OnClick(object sender, EventArgs e)
        {
            webUserControlResponsavel.Voltar();
            panelResponsavel.Visible = true;
            panelVisualizarResponsavel.Visible = false;
            gridViewResponsavel.DataBind();
        }


        protected void buttonCadastrar_OnClick(object sender, EventArgs e)
        {
            webUserControlResponsavel.CarregaTelaDeCadastro();
            panelVisualizarResponsavel.Visible = true;
            panelResponsavel.Visible = false;
        }

        protected void gridIdosos_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            
        }

        protected void buttonAddIdoso_OnClick(object sender, EventArgs e)
        {
            
        }

        protected void repeaterIdosos_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "visualizar")
            {
                int idIdoso = Convert.ToInt32(e.CommandArgument);
                if (idIdoso != null)
                {
                    MontaPaginaComDetalhesDoIdoso(idIdoso, false);
                    
                }
            }
        }

        private void MontaPaginaComDetalhesDoIdoso(int idIdoso, bool enable)
        {
            DataSourceIdoso dataSourceIdoso = new DataSourceIdoso();
            Business.Model.Idoso idoso = dataSourceIdoso.BuscaIdoso(idIdoso);
            CarregaVisualizacaoDetalhadaDoIdoso(idoso, enable);
        }

        private void CarregaVisualizacaoDetalhadaDoIdoso(Idoso idoso, bool enable)
        {
            
        }
    }
}